<template>
  <div class="center">
    <div class="search clearfix">
      <el-input
        v-model="groupname"
        :clearable="true"
        placeholder="请输入组名"
        :style="{ width: '200px' }"
      ></el-input>
      <el-select
        v-model="dicttype"
        :clearable="true"
        :filterable="true"
        placeholder="请选择字典类型"
        :style="{ width: '200px' }"
      >
        <el-option
          v-for="item in typelist"
          :key="item.index"
          :value="item.value"
        >
        </el-option>
      </el-select>
      <el-input
        v-model="dictname"
        :clearable="true"
        placeholder="请输入字典名"
        :style="{ width: '200px' }"
      ></el-input>
      <el-button type="primary" @click="getdict">查询</el-button>
      <el-button type="primary" @click="add">新增</el-button>
    </div>
    <div class="table">
      <el-table
        :data="tableData"
        class="customer-table"
        :header-cell-style="headClass"
      >
        <el-table-column type="index" width="50"></el-table-column>
        <el-table-column prop="id" label="id" width="50"> </el-table-column>
        <el-table-column prop="dictName" label="字典名称" align="center">
        </el-table-column>
        <el-table-column prop="dictType" label="字典类型" align="center">
        </el-table-column>
        <el-table-column prop="group" label="组名" align="center">
        </el-table-column>
        <el-table-column prop="value" label="字典值" align="center">
        </el-table-column>
        <el-table-column  label="创建时间" align="center">
          <template slot-scope="scope">
            {{scope.row.createTime.substring(0,10)}}
          </template>
        </el-table-column>
        <el-table-column prop="memo" label="备忘录" align="center">
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button size="mini" type="primary" @click="change(scope.row)"
              >编辑</el-button
            >
            <el-button
              size="mini"
              type="danger"
              @click="handleDelete(scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        @current-change="handleCurrentChange"
        @prev-click="prevclick"
        @next-click="nextclick"
        :current-page="currentPage"
        :page-size="pagesize"
        layout="total,prev, pager, next"
        :total="total"
      >
      </el-pagination>
    </div>

    <!-- 字典编辑 -->
    <el-dialog
      class="dict-update"
      title="字典编辑"
      :inline="true"
      :visible.sync="dialogFormVisible2"
      :append-to-body="true"
    >
      <el-form
        class="clearfix"
        :model="updateform"
        label-position="left"
        label-width="100px"
        ref="updateruleform"
        :rules="updaterules"
      >
        <el-form-item label="字典名称" prop="dictName">
          <el-input v-model="updateform.dictName"></el-input>
        </el-form-item>
        <el-form-item label="字典类型" prop="type">
          <el-select v-model="updateform.dictType">
            <el-option
              v-for="item in dicttypelist"
              :key="item.index"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="字典状态" prop="status">
          <el-select v-model="updateform.status">
            <el-option
              v-for="item in dictstatuslist"
              :key="item.index"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="备忘录" prop="memo">
          <el-input v-model="updateform.memo"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="updatedict(updateform)"
            >修改</el-button
          >
          <el-button @click="updateout">取 消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>

    <!-- 新增字典 -->
    <el-dialog
      class="dict-add"
      title="新增字典"
      :inline="true"
      :visible.sync="dialogFormVisible3"
      :append-to-body="true"
    >
      <el-form
        class="clearfix"
        :model="addform"
        label-position="left"
        label-width="100px"
        ref="addruleform"
        :rules="addrules"
      >
        <el-form-item label="字典名称" prop="dictName">
          <el-input v-model="addform.dictName"></el-input>
        </el-form-item>
        <el-form-item label="字典类型" prop="type">
          <el-select v-model="addform.type">
            <el-option
              v-for="item in dicttypelist"
              :key="item.index"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="字典组" prop="group">
          <el-select v-model="addform.group">
            <el-option
              v-for="item in dictgrouplist"
              :key="item.index"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="字典状态" prop="status">
          <el-select v-model="addform.status">
            <el-option
              v-for="item in dictstatuslist"
              :key="item.index"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="备忘录" prop="memo">
          <el-input v-model="addform.memo"></el-input>
        </el-form-item>
        <el-form-item label="字典值" prop="value">
          <el-input v-model="addform.value"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="adddict(addform)">新增</el-button>
          <el-button @click="addout">取 消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: "datadict",
  data() {
    return {
      currentPage: 1,
      pagesize: 10,
      total: 0,
      tableData: [],
      dictname: "",
      dicttype: "",
      typelist: [],
      dicttypelist: [],
      dictgrouplist: [
        {
          value: "文史类",
        },
        {
          value: "科学类",
        },
      ],
      dictstatuslist: [],
      groupname: "",
      dialogFormVisible3: false,//新增字典弹窗
      dialogFormVisible2: false,//字典编辑弹窗
      addform: {
        dictName: "",
        dictType: "",
        group: "",
        status: "",
        value: "",
        memo: "",
      },
      addrules: {
        dictName: [
          { required: true, message: "字典名称不能为空", trigger: "blur" },
        ],
        group: [
          { required: true, message: "字典组名不能为空", trigger: "blur" },
        ],
        value: [{ required: true, message: "字典值不能为空", trigger: "blur" }],
      },
      addruleform: {
        dictName: "",
        group: "",
        value: "",
      },
      updateform: {},
      updaterules: {
        dictName: [
          { required: true, message: "字典名称不能为空", trigger: "blur" },
        ],
      },
      updateruleform: {
        dictName: "",
      },
    };
  },
  watch:{
    currentPage(){
      this.getdict();
    }
  },
  methods: {
    headClass() {
      return "background-color: transparent;color:#fff;font-weight:bold;";
    },
    //获取字典列表
    getdict() {
      let params = {
        dictName: this.dictname || "",
        dictType: this.dicttype || "",
        group: this.groupname || "",
        page: {
          pageNo: this.currentPage,
          pageSize: this.pagesize,
        },
      };
      this.$http
        .post("/dict/list", params)
        .then((res) => {
          if (res.data.h.code == 0) {
            this.total = res.data.b.totalCount;
            this.tableData = res.data.b.result;
          }
        })
        .catch((err) => {});
    },
    add() {
      this.dialogFormVisible3 = true;
    },

    //新增字典表单清空
    addformclear() {
      for (let i in this.addform) {
        this.addform[i] = "";
      }
    },
    //新增字典结束
    addout() {
      this.$refs.addruleform.clearValidate();
      this.dialogFormVisible3 = false;
      this.addformclear();
    },
    //编辑字典结束
    updateout() {
      this.$refs.updateruleform.clearValidate();
      this.dialogFormVisible2 = false;
      this.updateformclear();
    },

    //编辑字典表单清空
    updateformclear() {
      for (let i in this.updateform) {
        this.updateform[i] = "";
      }
    },
    adddict(form) {
      let params = {
        dictName: form.dictName,
        dictType: form.dictType,
        group: form.group,
        status: form.status,
        value: form.value,
        memo: form.memo,
      };
      this.$refs.addruleform.validate((valid) => {
        if (valid) {
          this.$http
            .post("/dict/add", params)
            .then((res) => {
              if (res.data.h.code == 0) {
                this.addout();
                this.getdict();
                this.$message({
                  message: "新增成功",
                  type: "success",
                });
              }
            })
            .catch((err) => {});
        } else {
          this.$message({
            message: "表单填写有误，请重新填写",
            type: "warning",
          });
          return false;
        }
      });
    },
    handleChange(val) {
      this.type = val;
    },
    //上一页
    prevclick() {
      this.currentPage--;
    },
    //下一页
    nextclick() {
      this.currentPage++;
    },
    //分页
    handleCurrentChange(val) {
      this.currentPage = val;
    },
    //字典编辑开始
    change(row) {
      this.dialogFormVisible2 = true;
      this.updateform = Object.assign({}, row);
    },
    updatedict(form) {
      let params = {
        id: form.id,
        dictName: form.dictName,
        status: form.status,
        memo: form.memo,
      };
      this.$refs.updateruleform.validate((valid) => {
        if (valid) {
          this.$http
            .post("/dict/update", params)
            .then((res) => {
              if (res.data.h.code == 0) {
                this.updateout();
                this.getdict();
                this.$message({
                  message: "修改成功",
                  type: "success",
                });
              }
            })
            .catch((err) => {});
        } else {
          this.$message({
            message: "表单填写有误，请重新填写",
            type: "warning",
          });
          return false;
        }
      });
    },
    //字典删除
    handleDelete(row) {
      this.$confirm("此操作将删除该字典, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          let params = {
            id: row.id,
          };
          this.$http.post("/dict/del", params).then((res) => {
            if (res.data.h.code == 0) {
              this.$message({
                type: "success",
                message: "删除成功!",
              });
              //重新请求字典列表
              this.getdict();
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
  },
  mounted() {
    this.getdict();
  },
};
</script>

<style scoped>
.center {
  position: relative;
}
.search {
  padding-left: 20px;
  box-sizing: border-box;
  display: inline-block;
}
.search .el-select {
  float: left;
  width: 120px;
  margin-right: 10px;
}
.search .el-input {
  float: left;
  width: 150px;
  margin-right: 10px;
}
.block {
  display: inline-block;
  margin-right: 10px;
}
.table {
  margin: 0 20px;
  margin-top: 20px;
  box-sizing: border-box;
}
.el-pagination {
  text-align: center;
  margin-top: 20px;
  margin-bottom: 20px;
}
.dict-info .el-form {
  padding: 15px;
  box-sizing: border-box;
}
.dict-add .el-form {
  padding: 15px;
  box-sizing: border-box;
}
.dict-update .el-form {
  padding: 15px;
  box-sizing: border-box;
}
.dict-info .el-form .el-form-item .el-select {
  display: block;
}
.dict-add .el-form .el-form-item .el-select {
  display: block;
}
.dict-update .el-form .el-form-item .el-select {
  display: block;
}
</style>
<style>
.customer-table th {
  border: none;
}
.customer-table td,
.customer-table th.is-leaf {
  border: none;
}
</style>